import tkinter as tk
import tkinter.messagebox as tms
import threading
import Util.InIManager as iniManager
import random
from Util import WindowsUtil as Util
from RememberWord.DataBaseConnectHandle import Handler
from windows import WidgetConfig

from windows.SelectPane import SelectPane
from windows import WindowsMain


class Manager:
    def __init__(self, win: tk.Tk = None):
        self.win = win
        self.wino = ...
        self.Worry = 0
        self.username = ''
        self.count = 1
    def Check(self,index,value):
        if self.count=="MAX":
            self.h.WriteHistory(value,self.Worry)
            tms.showinfo('成功',f"共20题 你错了{self.Worry} 道")
            self.wino.Login2Menu()
            return
        self.count+=1
        if self.English[index].lower() != value.lower():
            self.Worry += 1
        if self.count > 20:
            self.Exam(str=True)


        else:
            self.Exam()


    def Exam(self, _=..., str=None):
        count = self.count
        self.wino.Login2Menu(True)
        self.TitleLable_ExamSelect = WidgetConfig.WidgetConfig("#2B2B2B", tk.Label(self.win, fg='white'),
                                                               "根据中文写英语，写完按Enter",
                                                               Util.Font(self.win).Title)
        self.TitleLable_ExamSelect.initFont()
        self.TitleLable_ExamSelect = self.TitleLable_ExamSelect.getWidget()
        self.TitleLable_ExamSelect.place(relwidth=1, x=0, y=120)

        random_index = random.randint(1,len(self.Chinese)-1)
        self.WORD_CNF = WidgetConfig.WidgetConfig("#2B2B2B", tk.Label(self.win, fg='white'),
                                                               f"{count} {self.Chinese[random_index]}"
                                                               if not str else "你的姓名(真实姓名）：",
                                                               Util.Font(self.win).Text)
        self.WORD_CNF.initFont()
        self.WORD_CNF_LABLE = self.WORD_CNF.getWidget()
        self.WORD_CNF_LABLE.place(relwidth=1, x=0, y=240)
        self.Entry = tk.Entry()
        self.Entry.focus_set()
        self.Entry.place(relwidth=.4, relx=0.3, y=300,height=30)
        if not str:
            self.win.bind("<Return>", lambda e:self.Check(random_index,self.Entry.get()))
        else:
            self.count = "MAX"
            self.win.bind("<Return>", lambda e:self.Check(random_index,self.Entry.get()))

    def Pack_Remember(self):
        WordList = self.h.FindAll()
        SettingInIManager = iniManager.iniManager("Setting.ini")  # 刷新管理器
        count_word = int(SettingInIManager.getValue("Setting", "wordcount"))
        if count_word < len(WordList):
            WordList = random.choices(WordList, k=count_word)
        else:
            tms.showinfo("提示", "现在词库尚未拥有设置中指定的单词数目，故而进行所有单词全部的复习")

        self.English, self.Chinese = [], []
        for i in WordList:
            self.English.append(i[0])
            self.Chinese.append(i[1])
        self.win.state = "背单词"
        self.TitleLable_ExamSelect = WidgetConfig.WidgetConfig("#2B2B2B", tk.Label(self.win, fg='white'),
                                                               "英语单词记忆",
                                                               Util.Font(self.win).Title)
        self.TitleLable_ExamSelect.initFont()
        self.TitleLable_ExamSelect = self.TitleLable_ExamSelect.getWidget()
        self.TitleLable_ExamSelect.place(relwidth=1, x=0, y=120)
        # TODO 选择SelectPane窗口搭建
        self.e = SelectPane(titleList=self.Chinese, infoList=self.English,
                            method=self.Exam,
                            background_color="#3C3F41", font_color="white",
                            win=self.win
                            , btn_value=["上个单词", '开始考试', "下个单词"],subtitleFont=("华文中宋",14),titleFont=("华文中宋", 19))
        self.s = self.e.getContainer()
        self.s.place(relx=0, y=210, relwidth=1, height=300)

    def start(self):
        self.Thread()

    def Thread(self):
        print("开始连接数据库")
        t1 = threading.Thread(target=self.mysqlConnect)
        t1.daemon = True
        t1.start()

    def mysqlConnect(self):

        self.TitleLable_ExamSelect = WidgetConfig.WidgetConfig("#2B2B2B", tk.Label(self.win, fg='white'),
                                                               "正在获取网络资源,请稍后...",
                                                               Util.Font(self.win).Title)
        self.TitleLable_ExamSelect.initFont()
        self.TitleLable_ExamSelect = self.TitleLable_ExamSelect.getWidget()
        self.TitleLable_ExamSelect.place(relwidth=1, x=0, rely=.5)

        self.h = Handler()
        self.h.connect()

        self.TitleLable_ExamSelect.destroy()
        self.Pack_Remember()
